/**
 * zmn 监控配置
 * 文件命名格式：zmn.模块名称.表名称.功能.js
 * @author chongw
 * @since 019/07/31 13:58
 */
let $;
layui.define(['index', 'form', 'table', 'laypage', 'laydate'], function (exports) {
    $ = layui.$;
    let form = layui.form;
    let layer = layui.layer;
    let table = layui.table;
    let laypage = layui.laypage;
    let laydate = layui.laydate;
    // 自定义验证规则
    form.verify({
        integer: [
            /^([0-9]|[0-9]\d)*$/
            , '只能输入正整数'
        ]
    });
    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        defaultToolbar: [],
        page: true,
        limit: json.pageSize,
        height: 'full-185',
        done: function () {

        }
    });

    // 渲染日期范围选择器
    laydate.render({
        elem: '#timeSearch',
        range: true,
        min: -90,
        max: 0,
        format: 'yyyy/MM/dd',
        trigger: 'click'
    });

    laypage.render(zmnPage.laypageOptions(json));

    // 启用/停用
    table.on("toolbar(zmn-table)", function (obj) {
        if (obj.event != "start" && obj.event != "stop") {
            return;
        }
        let ids = table.checkStatus('zmn-table').data.map((item) => item.sceneId);
        modifyStatus(obj, ids);
    });

    /**
     * 开启或关闭按钮
     */
    form.on("switch(chk_one_status)", function (obj) {
        let id = obj.elem.getAttribute("key");
        var ids = new Array();
        ids.push(id);
        let status = obj.elem.checked ? 2 : 1;
        var optObj = new Object();
        optObj.notConfirm = true;
        switch (status) {
            case 1:
                optObj.event = 'stop';
                modifyStatus(optObj, ids);
                break;
            case 2:
                optObj.event = 'start';
                modifyStatus(optObj, ids);
                break;
        }
    });

    function modifyStatus(obj, ids) {
        if (ids.length === 0) {
            return layer.alert("请选择至少一条数据！");
        }
        let url = "/brs/scene/enable.action";
        if (obj.event == "stop") {
            url = "/brs/scene/disable.action";
        }
        zmnPage.layStartStop(obj, url, ids.join(","));
    }

    $(".zmn-form-cancel").click(function () {
        let obj = {
            "sceneId": ""
            , "sceneName": ""
            , "type": ""
            , "timeSearch": ""
            , "status": ""
        };
        form.val('zmn-form', JSON.parse(JSON.stringify(obj)));
    });

    //对外暴露的接口
    exports('scene_list', {});
});



